function format(value, row, index) {
	var str = '<a  name="memorial-details" href="#" class="easyui-linkbutton edit-btn"></a>' + '<a  name="memorial-delete" href="#" class="easyui-linkbutton edit-btn"></a>'
			+ '<a  name="memorial-edit" href="#" class="easyui-linkbutton edit-btn"></a>';
	return str;

}

/* 清除数据表格中的数据 */
function clearDataGrid() {
	// 获取当前页的记录数
	var item = $('#jm-memorial-datagrid').datagrid('getRows');
	if (item != null) {
		for (var i = item.length - 1; i >= 0; i--) {
			var index = $('#jm-memorial-datagrid').datagrid('getRowIndex', item[i]);
			$('#jm-memorial-datagrid').datagrid('deleteRow', index);
		}
	}
}

$(function() {
	$("#jm-memorial-datagrid").datagrid({
		onLoadSuccess : function(data) {
			// datagrid的右侧的图片按钮
			// 视图
			$("a[name='memorial-details']").linkbutton({
				width : 30,
				iconCls : 'icon-my_detail',
				plain : true,
				onClick : function() {
					$("#jm-memorial-datagrid").datagrid({
						onClickCell : function(index) {
							var rows = $("#jm-memorial-datagrid").datagrid('getRows');//获得所有行
							var row = rows[index];//根据index获得其中一行
							if (row) {
								$('#view-memorial-form').form('clear');
								$('#view-memorial-dialog').window('open').panel({
									onLoad : function() {
										$('#view-memorial-form-memId').val(row.memorial.memId);
										$('#view-memorial-form-client-combobox').combobox({
											disabled : true
										});
										$('#view-memorial-form-client-combobox').combobox('setValue', row.contact.clientInfo.cid);
										$('#view-memorial-form-contact-combobox').combobox({
											disabled : true
										});
										$('#view-memorial-form-contact-combobox').combobox('setValue', row.contact.contactid);
										$('#view-memorial-form-memDate').datebox({
											disabled : true
										});
										$('#view-memorial-form-memDate').datebox('setValue', row.memorial.memDate);
										$('#view-memorial-form-type').textbox('setValue', row.memorial.type);
										$('#view-memorial-form-type').textbox({
											disabled : true
										});
										$('#view-memorial-form-remarks').textbox('setValue', row.memorial.remarks);
										$('#view-memorial-form-remarks').textbox({
											disabled : true
										});
										$('#view-memorial-form-nextMem').textbox('setValue', row.nextMem);
										$('#view-memorial-form-nextMem').textbox({
											disabled : true
										});
									}
								});
							} else {
								$.messager.alert("提示", "请先选中要编辑的行")
							}
							$("#jm-memorial-datagrid").datagrid({
								onClickCell : function(index) {
								}
							});
						}
					});
				}
			});
			// 删除
			$("a[name='memorial-delete']").linkbutton({
				width : 30,
				iconCls : 'icon-my_delete',
				plain : true,
				onClick : function() {
					$("#jm-memorial-datagrid").datagrid({
						onClickCell : function(index) {
							var rows = $('#jm-memorial-datagrid').datagrid('getRows');// 获得所有行
							var row = rows[index];// 根据index获得其中一行。
							$.messager.confirm("询问", "亲，您确定要删除吗？", function(v) {
								if (v) {
									var memId = row.memorial.memId;
									$.post('del-memorial', {
										memId : memId
									}, function(r) {
										if (r.success) {
											$.messager.alert("提示", "亲，删除成功！");
											$("#jm-memorial-datagrid").datagrid('reload');
										} else {
											$.messager.alert("提示", r.messager);
										}
									});
								}
							});
							$("#jm-memorial-datagrid").datagrid({
								onClickCell : function(index) {
								}
							});
						}
					});
				}
			});
			// 编辑
			$("a[name='memorial-edit']").linkbutton({
				width : 30,
				iconCls : 'icon-my_edit',
				plain : true,
				onClick : function() {
					$("#jm-memorial-datagrid").datagrid({
						onClickCell : function(index) {
							var rows = $("#jm-memorial-datagrid").datagrid('getRows');//获得所有行
							var row = rows[index];//根据index获得其中一行
							if (row) {
								$('#edit-memorial-form').form('clear');
								$('#edit-memorial-dialog').window('open').panel({
									onLoad : function() {
										$('#edit-memorial-form-memId').val(row.memorial.memId);
										$('#edit-memorial-form-client-combobox').combobox('setValue', row.contact.clientInfo.cid);
										$('#edit-memorial-form-contact-combobox').combobox('setValue', row.contact.contactid);
										$('#edit-memorial-form-memDate').datebox('setValue', row.memorial.memDate);
										$('#edit-memorial-form-type').textbox('setValue', row.memorial.type);
										$('#edit-memorial-form-remarks').textbox('setValue', row.memorial.remarks);
										$('#edit-memorial-form-nextMem').textbox('setValue', row.nextMem);
									}
								});
							} else {
								$.messager.alert("提示", "请先选中要编辑的行")
							}
							$("#jm-memorial-datagrid").datagrid({
								onClickCell : function(index) {
								}
							});
						}
					});
				}
			});
		}
	});

	/* 点击头部搜索框中的搜索框进行搜所 */
	$("#memorial-searchbox").searchbox({
		searcher : function() {
			// 获取快速搜所类型
			var searchname = $('#memorial-searchbox').searchbox("getName");
			// 获取搜索框中的值
			var searchVal = $("#memorial-searchbox").searchbox("getValue");

			if (searchname == 'memId') {
				var pattern = /^[1-9]\d*$/;
				if (!pattern.test(searchVal)) {
					alert('亲，您输入的编号格式不正确');
					return;
				}
				clearDataGrid();
				$('#jm-memorial-datagrid').datagrid({
					queryParams : null
				});
				$('#jm-memorial-datagrid').datagrid({
					url : 'query-memorial-by-memId',
					method : 'post',
					queryParams : {
						memId : searchVal
					},
				});
				$('#jm-memorial-datagrid').datagrid('reload');
			} else if (searchname == "contactName") {
				clearDataGrid();
				$('#jm-memorial-datagrid').datagrid({
					queryParams : null
				});
				$('#jm-memorial-datagrid').datagrid({
					url : 'query-memorial-by-contactName',
					method : 'post',
					queryParams : {
						contactName : searchVal
					},
				});
				$('#jm-memorial-datagrid').datagrid('reload');
			}
		}
	});

	//新建
	$('#memorial-tools-btn-add').linkbutton({
		onClick : function() {
			$('#add-memorial-dialog').window('open');
		}
	});

	//删除
	$('#memorial-tools-btn-remove').linkbutton({
		onClick : function() {
			var rows = $("#jm-memorial-datagrid").datagrid('getChecked');
			if (rows.length == 0) {
				$.messager.alert("提示", "亲，请先选择要删除的行！");
			} else {
				$.messager.confirm("询问", "亲，您确定要删除吗？", function(v) {
					if (v) {
						for (var i = 0; i < rows.length; i++) {
							if (rows[i]) {
								var memId = rows[i].memorial.memId;
								$.post('del-memorial', {
									memId : memId
								}, function(r) {
									if (r.success) {
										$("#jm-memorial-datagrid").datagrid('reload');
									} else {
										$.messager.alert("提示", r.messager);
									}
								});
							}
						}
					}
				});
			}
		}
	});

	//查询所有
	$('#memorial-tools-btn-selectAll').linkbutton({
		onClick : function() {
			$('#jm-memorial-datagrid').datagrid({
				url : 'query-all-memorial',
				method : 'post',
				queryParams : null
			});
		}
	});
})